<?php

namespace App\Models\Shop;

use App\Libraries\Tools\ConstVar;
use App\Models\BaseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ShopCoupon extends BaseModel
{


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'shop_id',
        'title',
        'content',
        'cover',
        'status',
        'rec_num',
        'price',
        'discount_price',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'created_at',
        'updated_at'
    ];

    protected $dateFormat = 'U';

    /**
     * 关联商家
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function shop(){
        return $this->hasOne('App\Models\Shop\Shop', 'shop_id', 'shop_id');
    }

    /**
     * 关联统计数据
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function statistics(){
        return $this->hasOne('App\Models\Shop\ShopCouponStatistic', 'coupon_id', 'id');
    }

    /**
     * 关联经理
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function managers(){
        return $this->hasMany('App\Models\Shop\ShopManagerFullDetail', 'shop_id', 'shop_id');
    }

    /**
     * 列表
     * @param $shop_id
     * @param $page
     * @param $size
     * @param $is_manager
     * @return mixed
     */
    public function coupons($shop_id, $page, $size, $is_manager){
        $time = time();
        $sql = $this->where('shop_id', $shop_id)
            ->where('status', 1)
            ->withOnly('shop', ['shop_id', 'city_id', 'title'])
            ->withOnly('statistics', ['coupon_id', 'sales']);
        if(!$is_manager){
            $sql = $sql->where('start_at', '<=', $time)
                ->where('end_at', '>=', $time);
        }
        $data['list'] = $sql->forPage($page, $size+1)
            ->get();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

    /**
     * 详情
     * @param $id
     * @param bool $with_managers
     * @param bool $with_shop
     * @return mixed
     */
    public function detail($id, $with_managers=false, $with_shop=false){
        $sql = $this->withOnly('statistics', ['coupon_id', 'sales']);
        if($with_managers){
            $sql = $sql->with('managers');
        }
        if($with_shop){
            $sql = $sql->withOnly('shop', ['shop_id', 'title', 'city_id']);
        }
        $data = $sql->where('id', $id)
            ->where('status', 1)
            ->first();
        return $this->after($data);
    }

    /**
     * 购买套餐前检查
     * @param Request $request
     * @param null $model_ext
     * @return array
     */
    public function beforeCheck(Request &$request, $model_ext=null){
        $coupon = $this->with('shop')->where('id', $request['id'])->where('status', 1)->first();
        if(empty($coupon)){
            $this->rs_format['code'] = 40400;
            return $this->rs_format;
        }
        if(empty($coupon->discount_price)){
            $this->rs_format['code'] = 40001;
            return $this->rs_format;
        }
        if(!empty($request['hong_bao_id'])){
            $hong_bao = $model_ext
                ->where('id', $request['hong_bao_id'])
                ->where('status', 0)
                ->where('type', ConstVar::$HONGBAO_TYPE_CASH)
                ->where('end_at', '>=', time())
                ->where('limit_money', '<=', $coupon->discount_price)
                ->whereIn('agent_city_id', [0, $coupon->shop->city_id])
                ->first();
            if(empty($hong_bao)){
                $this->rs_format['code'] = 40400;
                return $this->rs_format;
            }
            if($hong_bao->money >= $coupon->discount_price){
                $this->rs_format['code'] = 40001;
                return $this->rs_format;
            }
            $hong_bao->status = 1;
            $hong_bao->save();
        }
        $this->rs_format['data']['price'] = $coupon->discount_price;
        $this->rs_format['data']['body'] = $coupon->title;
        $this->rs_format['data']['ext_id'] = $coupon->shop_id;
        $this->rs_format['data']['hong_bao_id'] = $request['hong_bao_id']??0;
        $this->rs_format['data']['hong_bao_amount'] = $hong_bao->money??0;
        return $this->rs_format;
    }
}
